#include "MP3BoFang.h"

int SZ_YINJIAO;

//获取引脚
void MP3BoFang::Huoquyinjiao(int yinjiao)
{
  SZ_YINJIAO = yinjiao;
}
//传输数据给模块——播报
void MP3BoFang::send_data(int addr){
  int i;
  digitalWrite(SZ_YINJIAO , LOW);
  delay(3); //>2ms
  for(i=0; i<8; i++){
    digitalWrite(SZ_YINJIAO, HIGH);
    if(addr&1){
      delayMicroseconds(2400); //>2400us
      digitalWrite(SZ_YINJIAO, LOW);
      delayMicroseconds(800);
    } //>800us
    else{
      delayMicroseconds(800); //>800us
      digitalWrite(SZ_YINJIAO, LOW);
      delayMicroseconds(2400);
    } //>2400us
    addr>>=1;
  }
  digitalWrite(SZ_YINJIAO, HIGH);
}

//根据输入值播报数字
void MP3BoFang::Bobaoshuzi(int SHUZI,int DELAY)
{
  int Qianwei,Baiwei,Shiwei,Gewei;
  /*----1位数----*/
  if(SHUZI>=0 && SHUZI<=9)
  {
    shuzibianma(SHUZI);
    delay(DELAY);
  }
  /*----2位数----*/
  else if(SHUZI>=10 && SHUZI<=99)
  {
    Shiwei = SHUZI/10;
    Gewei = SHUZI%10;
    if(Shiwei>1)
    {
      shuzibianma(Shiwei);
      delay(DELAY);
    }
    send_data(0x22);
    delay(DELAY);
    if(Gewei > 0)
    {
      shuzibianma(Gewei);
      delay(DELAY);
    } 
  }
  /*----3位数----*/
  else if(SHUZI>=100 && SHUZI<=999)
  {
    Baiwei = SHUZI/100;
    Shiwei = SHUZI/10%10;
    Gewei = SHUZI%10;
    shuzibianma(Baiwei);
    delay(DELAY);
    send_data(0x21);
    delay(DELAY); 
    if(Shiwei>0)
    {
      shuzibianma(Shiwei);
      delay(DELAY);
      send_data(0x22);
      delay(DELAY);
    }
    else if(Shiwei==0)
    {
      if(Gewei > 0) 
      {
        shuzibianma(Shiwei);
        delay(DELAY);
      }
    }
    if(Gewei > 0)
    {
      shuzibianma(Gewei);
      delay(DELAY);
    } 
  }
  /*----4位数----*/
  else if(SHUZI>=1000 && SHUZI<=9999)
  {
    Qianwei = SHUZI/1000;
    Baiwei = SHUZI/100%10;
    Shiwei = SHUZI%100/10;
    Gewei = SHUZI%10;
    /*----------千位----------*/
    shuzibianma(Qianwei);
    delay(DELAY);
    send_data(0x20);
    delay(DELAY);
    /*----------百位----------*/
    if(Baiwei>0)
    {
      shuzibianma(Baiwei);
      delay(DELAY);
      send_data(0x21);
      delay(DELAY);
    }
    else if(Baiwei==0)
    {
      if(Shiwei > 0) 
      {
        shuzibianma(Baiwei);
        delay(DELAY);
      }
    }
    /*----------十位----------*/
    if(Shiwei>0)
    {
      shuzibianma(Shiwei);
      delay(DELAY);
      send_data(0x22);
      delay(DELAY);
    }
    else if(Shiwei==0)
    {
      if(Gewei > 0) 
      {
        shuzibianma(Shiwei);
        delay(DELAY);
      }
    }
    /*----------个位----------*/
    if(Gewei > 0)
    {
      shuzibianma(Gewei);
      delay(DELAY);
    } 
  }
}
//0-9数字对应编码
void MP3BoFang::shuzibianma(int shuzi)
{
  switch(shuzi)
  {
    case 0:send_data(0x2C);break;
    case 1:send_data(0x23);break;
    case 2:send_data(0x24);break;
    case 3:send_data(0x25);break;
    case 4:send_data(0x26);break;
    case 5:send_data(0x27);break;
    case 6:send_data(0x28);break;
    case 7:send_data(0x29);break;
    case 8:send_data(0x2A);break;
    case 9:send_data(0x2B);break;
  }
  /*
  千_0x20
  百_0x21
  十_0x22
  */
}

//音量调节
void MP3BoFang::yinliang(int YL)
{
  switch(YL)
  {
    case 1:send_data(0xE0);break;
    case 2:send_data(0xE1);break;
    case 3:send_data(0xE2);break;
    case 4:send_data(0xE3);break;
    case 5:send_data(0xE4);break;
    case 6:send_data(0xE5);break;
    case 7:send_data(0xE6);break;
    case 8:send_data(0xE7);break;
  }
  delay(50);
} 